package ezxpns;

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.*;
import ezxpns.cmd.*;
import ezxpns.gui.menu.*;
import ezxpns.gui.tool.*;

public class Application extends JFrame
{
	private static final long serialVersionUID=1L;
	private JMenuBar menubar;
	private JToolBar toolbar;
	private JTabbedPane tabs;
	private CmdBase cmd;
	private AppKey app;
	public class AppKey
	{
		public Application root;
		public JMenuBar menubar;
		public JToolBar toolbar;
		public JTabbedPane tabs;
		public CmdBase cmd;
		public void update()
		{
			root=Application.this;
			menubar=Application.this.menubar;
			toolbar=Application.this.toolbar;
			tabs=Application.this.tabs;
			cmd=Application.this.cmd;
		}
	}
	private class Change implements ChangeListener
	{
		public void stateChanged(ChangeEvent event)
		{
			app.cmd.update();
		}
	}
	Application() throws Exception
	{
		// Set window properties //
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		UIManager.getDefaults().put("TabbedPane:TabbedPaneTabArea.contentMargins", new InsetsUIResource(30, 10, 0, 10));
		setTitle("Easy Expense");
		setExtendedState(MAXIMIZED_BOTH);
		setSize(512,384);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		// Create application key //
		app=new AppKey();
		// Create menubar //
		menubar=new JMenuBar();
		setJMenuBar(menubar);
		// Create toolbar //
		toolbar=new JToolBar();
		toolbar.setFloatable(false);
		add(toolbar,BorderLayout.PAGE_START);
		// Create file tab-pane //
		tabs=new JTabbedPane();
		tabs.setFocusable(false);
		tabs.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		add(tabs);
		// Create command base //
		app.update();
		cmd=new CmdBase(app);
		app.update();
		// Create menubar GUI //
		new MenuFile(app);
		new MenuEdit(app);
		// Create toolbar GUI //
		new ToolFile(app);
		// Make command base aware of changes in tab-pane //
		tabs.addChangeListener(new Change());
		// Show window //
		setVisible(true);
	}
	public static void main(String[] args) throws Exception
	{
		SwingUtilities.invokeLater
		(
			new Runnable()
			{
				public void run()
				{
					try
					{
						new Application();
					}
					catch(Exception e)
					{
						e.printStackTrace();
					}
				}
			}
		);
	}
}
